package com.neuedu.service.impl;

import com.neuedu.entity.User;
import com.neuedu.mapper.UserMapper;
import com.neuedu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: MARK
 * @Date: 2019/7/24 18:25
 * @version: 1.0.0
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询父级up字段
     * 添加username, WeChat, grade, pid
     * 对于up和down追加wechat, 而不用id，可以比追加id少一步查询id(因为id是自增字段), wechat也是唯一的
     * @param user
     */
    @Override
    public void add(User user) {
        //得到父级的up字段
        String parentUp = userMapper.selectParentUp(user.getPid());
        //得到父级的down字段
        String parentDown = userMapper.selectParentDown(user.getPid());
        //如果父级down为空，进行处理
        parentDown = parentDown == null? "": parentDown;
        //得到自己的WeChat字段用于查出的id
        String wechat = user.getWechat();
        //追加自己的wechat构建父级更新后的down
        parentDown = parentDown + "/" + wechat;
        String up = parentUp + "/" + wechat;
        //在父级up字段追加自己的wechat构造自己的up字段
        user.setUp(up);
        //插入自己完整的信息
        System.out.println("========="+user);
//        userMapper.insert(user);
        //修改父类down信息
        User parentUser = new User();
        parentUser.setId(user.getPid());
        parentUser.setDown(parentDown);
//        userMapper.updateParentDown(parentUser);
    }

    @Override
    public List<User> getParent(Integer id) {
        String up = userMapper.selectParentUp(20);
        String[] parents = up.split("/");
        List<User> list = new ArrayList<>();
        for (int i = 1; i < parents.length-1; i++) {
            System.out.println(parents[i]);
            User user = userMapper.selectAllInfo(parents[i]);
            list.add(user);
        }
        System.out.println((parents.length-2)+"");
        System.out.println(list);
        return list;
    }

    @Override
    public int getParentCount(String username) {
        return 0;
    }


}
